package com.content_module.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.content_module.R;
import com.content_module.R2;
import com.content_module.adapter.ImagePickerAdapter;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;

import butterknife.BindView;
import component.fy.com.common_module.base.BaseActivity;
import component.fy.com.common_module.baseAdapter.OnBaseAdapterItemClick;
import component.fy.com.common_module.utils.ToastUtil;
import component.fy.com.common_module.widget.MyGridItemDecoration;

public class ImagepickerAct extends BaseActivity {
    @BindView(R2.id.common_smart_refresh_layout_smart)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R2.id.common_smart_refresh_layout_recycler)
    RecyclerView recyclerView;

    private ImagePickerAdapter adapter;
    private static final int REQUEST_CODE_SELECT = 100;
    private static final int REQUEST_CODE_PREVIEW = 101;
    private ArrayList<ImageItem> selImageList = new ArrayList<>(); //当前选择的所有图片

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    adapter.setData(selImageList);
                    break;
            }
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.content_act_imagepicker;
    }

    @Override
    protected void initView() {
        recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        recyclerView.addItemDecoration(new MyGridItemDecoration(18, 4, false));
        recyclerView.setNestedScrollingEnabled(false);
        adapter = new ImagePickerAdapter(this, R.layout.content_item_image);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initEvents() {
        adapter.setOnBaseAdapterItemClick(new OnBaseAdapterItemClick() {
            @Override
            public void onItemClick(View v, int position) {
                super.onItemClick(v, position);
                if (position == 0) {
                    openImagePicker();
                }
            }

            @Override
            public void onItemLongClick(View v, int position) {
                super.onItemLongClick(v, position);
                if (position != 0) {
                    selImageList.remove(position);
                    handler.sendEmptyMessage(0);
                    ToastUtil.showToast("删除第" + position + "张照片");
                }
            }
        });
    }

    @Override
    protected void initData() {
        selImageList.add(new ImageItem());
        handler.sendEmptyMessage(0);
    }

    private void openImagePicker() {
        //打开选择,本次允许选择的数量
        ImagePicker.getInstance().setSelectLimit(10 - selImageList.size());
        Intent intent1 = new Intent(ImagepickerAct.this, ImageGridActivity.class);
        /* 如果需要进入选择的时候显示已经选中的图片，
         * 详情请查看ImagePickerActivity
         * */
//                                intent1.putExtra(ImageGridActivity.EXTRAS_IMAGES,images);
        startActivityForResult(intent1, REQUEST_CODE_SELECT);
    }

    private ArrayList<ImageItem> images = null;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            //添加图片返回
            if (data != null && requestCode == REQUEST_CODE_SELECT) {
                images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                if (images != null) {
//                    selImageList.clear();
//                    selImageList.add(new ImageItem());
                    selImageList.addAll(images);
                    handler.sendEmptyMessage(0);
//                    showLoading("正在上传头像...");
//                    upLoadFace();//上传头像
                }
            }
        } else if (resultCode == ImagePicker.RESULT_CODE_BACK) {
            //预览图片返回
            if (data != null && requestCode == REQUEST_CODE_PREVIEW) {
                images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_IMAGE_ITEMS);
                if (images != null) {
//                    selImageList.clear();
//                    selImageList.add(new ImageItem());
                    selImageList.addAll(images);
                    handler.sendEmptyMessage(0);
//                    showLoading("正在上传头像...");
//                    upLoadFace();//上传头像
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        clearList(selImageList, images);
    }
}
